package ru.ifmo.rybakov.muclumon;

import java.util.Collection;
import java.util.Iterator;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

@Entity
@DiscriminatorValue("U")
public class Cluster extends Element {

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "cluster", fetch = FetchType.EAGER)
	private Collection<Node> nodes;

	/**
	 * Returns the nodes
	 * @return The nodes
	 */
	public Collection<Node> getNodes() {
		return nodes;
	}

	/**
	 * Sets the nodes value
	 * @param nodes The nodes to be set
	 */
	public void setNodes(Collection<Node> nodes) {
		this.nodes = nodes;
	}

	public Node getNodeByName(String name) {
		assert(name != null);
		assert(nodes != null);
		Iterator<Node> it = nodes.iterator();
		while (it.hasNext()) {
			Node node = it.next();
			if (name.equals(node.getName())) {
				return node;
			}
		}
		return null;
	}
}
